<!-- 户籍信息 -->
<template>
  <zh-form :transprent="true" v-model="form" inputAlign="right" :label-width="200" :margin="[0, 24]" :padding="[10, 0]">
    <zh-card :width="620">
      <template v-slot:title>
        <view style="border-left: 10rpx solid #2f54eb">
          <zh-text :fontSize="36" class="text-weight-b pl-10" :label="form.fullName"></zh-text>
        </view>
      </template>
      <template v-slot:status>
        <tm-icon :fontSize="42" :name="data.hiddenFlag ? 'tmicon-eye-slash' : 'tmicon-eye'" style="margin: 0 20rpx" @tap="changeSensitive"></tm-icon>
      </template>
      <template v-slot:content>
        <zh-form-desc
          v-if="form.householdAddress"
          field="householdAddress"
          placeholder="未获取到东阳户籍信息"
          v-model="form.householdAddress"
          label="户籍地址"
          :transprent="true"
        ></zh-form-desc>
        <zh-form-desc v-if="form.householdAddress" field="relation" placeholder="未知" v-model="form.relation" label="与户主关系" :transprent="true"></zh-form-desc>
        <zh-form-desc v-if="form.householdAddress" field="accountType" placeholder="未知" v-model="form.accountType" label="户口类型" :transprent="true"></zh-form-desc>
        <zh-form-desc v-if="false" field="householdTime" placeholder="空" v-model="form.householdTime" label="户籍获取时间" :transprent="true"></zh-form-desc>
        <view v-if="props.myCheckInfo.hjCheck.dsjFlag && !props.myCheckInfo.bmFlag && !form.bmFlag">
          <zh-sheet v-if="form.householdAddress" :margin="[0, 20]" :shadow="3" :round="5" :padding="[20, 20]" color="deep-orange" text>
            <zh-text >如果新生户籍信息与实际不符,可点击<span style="color:#3B5CF0;font-weight: 600;" @click="getHjInfo">重新获取</span>,每天限获取一次。</zh-text>
          </zh-sheet>
          <zh-sheet  v-if="!form.householdAddress">
							<zh-result  title="未获取到户籍信息" :subTitle="(!props.myCheckInfo.bmFlag&&props.myCheckInfo.hjCheck.dsjFlag)?'非东阳市户籍新生不显示户籍信息。东阳市户籍新生可重新获取，每天限获取一次。':'非东阳市户籍新生不显示户籍信息。'" :showBtn="!props.myCheckInfo.bmFlag&&props.myCheckInfo.hjCheck.dsjFlag"  btnText="重新获取" @click="getHjInfo"></zh-result>
					</zh-sheet>
        </view>
        <view v-if="!props.myCheckInfo.hjCheck.dsjFlag  && !props.myCheckInfo.bmFlag && !form.bmFlag">
          <zh-sheet  v-if="!form.householdAddress">
							<zh-result  title="未获取到户籍信息" :subTitle="(!props.myCheckInfo.bmFlag&&props.myCheckInfo.hjCheck.dsjFlag)?'非东阳市户籍新生不显示户籍信息。东阳市户籍新生请耐心等待同步':'非东阳市户籍新生不显示户籍信息。'" :showBtn="false"  btnText="前往申诉" ></zh-result>
					</zh-sheet>
          <zh-sheet v-if="form.householdAddress" :margin="[0, 20]" :shadow="3" :round="5" :padding="[20, 20]" color="deep-orange" text>
            <zh-text >如果新生户籍信息与实际不符,可致电联系。</zh-text>
          </zh-sheet>
        </view>
        <zh-sheet v-if="props.myCheckInfo.bmFlag&&!form.householdAddress" :margin="[30, 20]"  :shadow="3" :round="5" :padding="[20, 20]" color="deep-orange" text>
          <zh-text >在东阳没有户籍信息。</zh-text>
        </zh-sheet>          
        <view v-if="props.myCheckInfo.hjCheck.showXqFlag && form.householdAddress" style="padding-top: 40rpx">
          <view style="border-left: 10rpx solid #2f54eb">
            <zh-text :fontSize="36" class="text-weight-b pl-10" label="户籍所在学区信息"></zh-text>
          </view>
          
          <view
            :class="!elderFlag ? 'flex-row-center-start' : ''"
            style="padding-top: 20rpx"
            :key="'schoolHousehold' + index"
            v-for="(schoolItem, index) in form.schoolDistrictVOList"
          >
            <view class="flex-row-center-start">
              <tm-icon name="tmicon-home" :fontSize="30" color="primary"></tm-icon>
              <zh-text class="pl-5" :fontSize="30" :label="schoolItem.schoolName"></zh-text>
            </view>
            <zh-tag v-if="schoolItem.nativeFlag === '1'" color="light-blue" size="n" :followTheme="false" label="原住民可就读"></zh-tag>
          </view>
          <zh-sheet v-if="props.myCheckInfo.hjCheck.ssXqFlag && !props.myCheckInfo.bmFlag && !form.bmFlag" :margin="[0, 20]" :shadow="3" :round="5" :padding="[20, 20]" color="deep-orange" text>
            <zh-text >如果新生户籍对应学区有误，可<span style="color:#3B5CF0;font-weight: 600;" @click="gotoHjXqAppeal">前往申诉</span>。如果学区未显示请联系学校。</zh-text>
          </zh-sheet>
          <zh-sheet v-if="!props.myCheckInfo.hjCheck.ssXqFlag && !props.myCheckInfo.bmFlag && !form.bmFlag" :margin="[0, 20]" :shadow="3" :round="5" :padding="[20, 20]" color="deep-orange" text>
            <zh-text >如果新生户籍对应学区有误，请联系学校。</zh-text>
          </zh-sheet>
        </view>
      </template>
    </zh-card>
    
  </zh-form>
</template>
<script lang="ts" setup>
import { system } from '@/store';
import * as util from '@/tool/function/util';
import * as dayjs from 'dayjs/esm/index';
const DayJs = dayjs.default;
const props = defineProps({
  myCheckInfo: {
    type: Object,
    default: () => {},
  },
  //户籍信息
  modelValue: {
    type: Object,
    default: () => {},
  },
});
// 主数据
const emits = defineEmits(['showMessage']);
const elderFlag = computed(() => {
  return system.uiStyle === 'elder';
});
let form = ref<any>(Object.assign({}, props.modelValue));
let oldForm = ref<any>(Object.assign({}, props.modelValue));
const data = reactive({
  //脱敏
  hiddenFlag: true,
});

const changeSensitive = () => {
  data.hiddenFlag = !data.hiddenFlag;
  enInfo();
};
const enInfo = () => {
  if (data.hiddenFlag) {
    form.value.fullName = getEnName(form.value.fullName);
    form.value.idcard = getEnIdcard(form.value.idcard);
  } else {
    form.value.fullName = oldForm.value.fullName;
    form.value.idcard = oldForm.value.idcard;
  }
};
//姓名加密
const getEnName = (fullName: any) => {
  if (!fullName) {
    return '';
  }
  return '*' + fullName.substring(1, fullName.length);
};
//身份证加密
const getEnIdcard = (idCard: any) => {
  if (!idCard) {
    return '';
  }
  let enCard = '';
  if (idCard.length === 18) {
    enCard = idCard.replace(/(\w{1})\w*(\w{1})/, '$1****************$2');
  } else {
    let len = idCard.length / 3;
    let baseLen = idCard.length - len * 2;
    enCard = idCard.substring(0, len);

    for (var i = 0; i < baseLen; i++) {
      enCard = enCard + '*';
    }
    enCard = enCard + idCard.substring(len * 2, idCard.length);
  }
  return enCard;
};
const getHjInfo = () => {
  //获取户籍信息
  let hjcountMap = util.getCookie('getHjCount:' + oldForm.value.idcard);
  let curData = DayJs(new Date()).format('YYYYMMDD');
  let flag = true;
  let count = 1;
  if (hjcountMap) {
    let json = hjcountMap;
    count = json[curData] || 1;
    if (json[curData] && Number(json[curData]) >= 1) {
      flag = false;
      let messageData: any = {};
      messageData.model = 'warn';
      messageData.text = '今日户籍获取已达上限，请明天再试！';
      emits('showMessage', messageData);
      return;
    }
  }
  if (flag) {
    let loacalFlag=false;
    $api.common.send
      .post('eduHousehold0008', { idcard: oldForm.value.idcard, studentEnrollPreId: form.value.studentEnrollPreId,localFlag:loacalFlag },null,true)
      .then((res: any) => {
        let map = {} as any;
        map[curData]=count + 1
        util.setCookie('getHjCount:' + oldForm.value.idcard, map);
        if(!loacalFlag){
          let messageData: any = {};
          messageData.model = 'success';
          messageData.text = '获取成功！请耐心等待2小时候查看结果';
          emits('showMessage', messageData);
        }else{
          if (res.result) {
            form.value.householdId = res.result.householdId;
            form.value.householdAddress = res.result.householdAddress;
            form.value.householdId = DayJs(res.result.createTime).format('YYYY年MM月DD日');
            let messageData: any = {};
            messageData.model = 'success';
            messageData.text = '获取成功！';
            emits('showMessage', messageData);
          } else {
            form.value.householdId = '';
            form.value.householdAddress = '';
            let messageData: any = {};
            messageData.model = 'warn';
            messageData.text = '未获取到您孩子的东阳户籍数据';
            emits('showMessage', messageData);
          }
        }
        
      })
      .catch(() => {});
  }
};
const gotoHjXqAppeal = () => {
  //前往户籍学区申诉
  $router.push('/pages/edu/appeal/appeal', { type: '4', id: form.value.householdId });
};
//前往我的申诉
const gotoMyAppeal = () => {
  $router.replace('/pages/edu/appeal/myAppeal', {});
};
// 初始化
onMounted(() => {
  enInfo();
});
watch(
  () => props.modelValue,
  () => {
    form.value = Object.assign({}, props.modelValue);
    oldForm.value = Object.assign({}, props.modelValue);
    enInfo();
  },
  { deep: true }
);
</script>
<style lang="scss" scoped></style>
